<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * create pagecounter-code for articlepages
 * get: pagecounter.tpl.php
 * used from category.php
 *
 * @version    $Id$
 * @package    TobbiVM-Shop
 * @copyright  Copyright (C) 2012
 * @author     Norbert Gebert
 * @license    GPL3
 */
$resultz = db::getInstance()->rawQuery("SELECT count(*) FROM product " .
	"WHERE product_category = '" . $title . "' AND product_active = '1'");
$productNumber = intval($resultz[0]['count(*)']);

$cols = Array('product_id');
db::getInstance()->where('product_category', $title);
$productIds = db::getInstance()->getOne("product", null, $cols);

if (!isset($_GET['number']) or $refValidate->testNumber($_GET['number'], '', 1, 200) === FALSE)
{
	$acPage = 1;
	$_GET['number'] = 1;
}

if ($toShow == 0)
{
	// generate setings for all products to display
	$displayStart = 1;
	$displayEnd = $productNumber;
	$refPage->assign('pagecounter', FALSE);
}
else
{
	$pages = ceil($productNumber / $toShow);
	$refPage->assign('pagecounter', TRUE);
	if (intval($_GET['number']) >= $pages)
	{
		$acPage = $pages;
	}
	else
	{
		$acPage = intval($_GET['number']);
	}

	// list of prevoious pages
	$html = '';
	if ($acPage - 1 >= 1)
	{
		for ($a = $acPage - 1; $a >= 1; $a--)
		{
			$html = '<a href="' .
				'?page=' . $page . '&title=' . $title . '&name=' . $name . '&number=' . $a . '" ' .
				'rel="tag">' .
				'<span class="pagecounter_number" title="zu Seite ' .
				$a . '">&nbsp;' . $a . '&nbsp;</span></a> ' . $html;
		}
	}
	else
	{
		$html = '';
	}
	$refPage->assign('previous', $html);

	// actual page
	$refPage->assign('displaypage',
		'<span class="pagecounter_number_ak" title="aktuelle Seite">&nbsp;' . $acPage .
		'&nbsp;</span>');

	// list of next pages
	$html = '';
	if ($acPage + 1 <= $pages)
	{
		for ($a = $acPage + 1; $a <= $pages; $a++)
		{
			$html .= '<a href="' .
				'?page=' . $page . '&title=' . $title . '&name=' . $name . '&number=' . $a . '" ' .
				'rel="tag">' .
				'<span class="pagecounter_number" title="zu Seite ' .
				$a . '">&nbsp;' . $a . '&nbsp;</span></a> ';
		}
	}
	else
	{
		$html = '';
	}
	$refPage->assign('next', $html);

	// first page
	$link = '?page=' . $page . '&title=' . $title . '&name=' . $name . '&number=1';
	$refPage->assign('firstpage',
		'<a href="' . $link . '" title="zur ersten Seite" rel="tag">' .
		'<div class="pagecounter_button_l">' .
		'<< </div></a>');

	// previous page
	if ($pages > 1 and $acPage > 1)
		$link = '?page=' . $page . '&title=' . $title . '&name=' . $name . '&number=' . ($acPage - 1);
	$refPage->assign('peviouspage',
		'<a href="' . $link . '" title="eine Seite zurück" rel="tag">' .
		'<div class="pagecounter_button_r">' .
		'< </div></a>');

	// next page
	if ($pages > 1 and $acPage < $pages)
	{
		$link = '?page=' . $page . '&title=' . $title . '&name=' . $name . '&number=' . ($acPage + 1);
	}
	else
	{
		$link = '?page=' . $page . '&title=' . $title . '&name=' . $name . '&number=' . $pages;
	}
	$refPage->assign('nextpage',
		'<a href="' . $link . '" title="eine Seite vorwärts" rel="tag">' .
		'<div class="pagecounter_button_l">' .
		'> </div></a>');

	// last page
	$link = '?page=' . $page . '&title=' . $title . '&name=' . $name . '&number=' . $pages;
	$refPage->assign('lastpage',
		'<a href="' . $link . '" title="zur letzten Seite" rel="tag">' .
		'<div class="pagecounter_button_r">' .
		'>> </div></a>');

	// generate setings for products to display
	$displayStart = (($acPage * $toShow) + 1) - $toShow;
	$displayEnd = ($displayStart + $toShow) - 1;
}
?>